import React, {useState} from 'react';
import { LockOutlined, UserOutlined } from '@ant-design/icons';
import {Button, Checkbox, Form, Input, message} from 'antd';
import styles from "./index.module.less";
import {useNavigate} from "react-router-dom";
import {postLogin} from "@/api/auth";
const Login: React.FC = () => {
    const navigate = useNavigate();
    const [loading,setLoading] = useState(false);
    const onFinish = async (values: any) => {
        try{
            setLoading(true);// 正在加载中
            const result:any = await postLogin(values);
            message.success(result.message);
            // 保存token
            localStorage.setItem("token",result.data.token)
            // 跳转至主页
            navigate("/");
        }catch (err){
            setLoading(false);
        }

    };

    return (
        <div className={styles.login}>
            <div>
                <h1>尚医通管理系统</h1>
                <Form
                    name="normal_login"
                    className="login-form"
                    initialValues={{ remember: true }}
                    onFinish={onFinish}
                >
                    <Form.Item
                        name="username"
                        rules={[{ required: true, message: '请输入管理员账号!' }]}
                    >
                        <Input prefix={<UserOutlined className="site-form-item-icon" />} placeholder="管理员账号" />
                    </Form.Item>
                    <Form.Item
                        name="password"
                        rules={[{ required: true, message: '请输入管理员密码' }]}
                    >
                        <Input
                            prefix={<LockOutlined className="site-form-item-icon" />}
                            type="password"
                            placeholder="管理员密码"
                        />
                    </Form.Item>

                    <Form.Item>
                        <Button loading={loading} type="primary" htmlType="submit" className="login-form-button">
                            登陆
                        </Button>
                    </Form.Item>
                </Form>
            </div>

        </div>

    );
};

export default Login;